--======================================================================
--（c）copyright 2015 175game.com All Rights Reserved
--======================================================================
-- modifier: hy
-- descrip: 协议封包和解包
--======================================================================

-- type:
-- 0:int, 1:string, 2:struct, 3:double, 6:int8, 7:int16, 8:float
local Tool = Import("logic/common/tool").CTool

local kPtoFilePath = "logic/config/rpc/rpc_lua_table"

Crpc = class()
Crpc.__name = "Crpc"

Crpc.Init = function(self, protocolMgr)
	self._protocolMgr = protocolMgr
	self._server_protocols = {}		--上行协议列表
	self._isPrintLog = GlobalCfg.IsRpcLog 		--是否输出Rpc日志
	self._rpcConfigMd5 = ""
	self:LoadConfig()
end

--加载协议配置
Crpc.LoadConfig = function(self, str)
	if str then
		local truck = loadstring(str)
		if type(truck) == "function" then
			self._rpcConfigMd5 = UtilCommon.HashToMD5Hex(str)
			self._rpc_data = truck()
		else
			print("update rpc error!", debug.traceback())
		end
	else
		--local ptoPath = Util.GetAssetsDataPath(kPtoFilePath..".lua", "lua")--Util.LuaPath(kPtoFilePath..".lua")
		--local content = UtilCommon.ReadAllTextFromFile(ptoPath)
		--self._rpcConfigMd5 = UtilCommon.HashToMD5Hex(content)

		local ptoPath = kPtoFilePath..".lua"
		local content = Util.ReadLuaFile(ptoPath)
		self._rpcConfigMd5 = UtilCommon.HashToMD5Hex(content)
		self._rpc_data = safe_dofile(kPtoFilePath)
	end

	if not self._rpc_data then error_stack("协议文件解析失败, rpc_lua_table冲突了？") return end
	
	ioo.networkManager:InitSocketJson(Tool.Table2Json(self._rpc_data))

	self:ParseProtocol()			--解析协议信息
	
	self:GenServerProxy()
end

Crpc.GetPtoMd5 = function(self)
	local ptoPath = Util.GetAssetsDataPath(kPtoFilePath..".lua", "lua")--Util.LuaPath(kPtoFilePath..".lua")

	print("Crpc.GetPtoMd5 path:"..ptoPath)
	print("Crpc.GetPtoMd5 md5"..(self._rpcConfigMd5))
	return self._rpcConfigMd5
end

Crpc.UpdatePto = function(self, pto)
	--重新生成协议
	self:LoadConfig(pto)
end

Crpc.ParseProtocol = function(self)
	self._server_protocols = {}
	for k, v in pairs(self._rpc_data.function_cfg) do
		local func_info = 
		{
			id	= tonumber(k),
			name = v.function_name,
			args = v.args,
		}
		
		if v.function_name:find("rpc_server") then
			self._server_protocols[func_info.id] = func_info
		end
	end
end

-- 上行协议LOG输出屏蔽列表
local MASK_ASK_RPC_NAME_LIST = {
	["rpc_server_heartbeat"] = true,
	["rpc_server_move"] = true,
}

Crpc.GenServerProxy = function(self)
	for _,func_info in pairs(self._server_protocols) do
		local proxy_func = function(...)
			local args = {...}

			if self._isPrintLog and not MASK_ASK_RPC_NAME_LIST[func_info.name] then
				print(func_info.name, ":", func_info.id, "@", os.clock() , ":", table.get_table_str(args))
			end

			ioo.networkManager:SendRpc(func_info.id, args)
		end

		self._protocolMgr:GetProtocol()[func_info.name] = proxy_func
	end
end

-- --解析与执行下行协议
-- --参数为包含参数的table
-- Crpc.OnPacket = function(self, funcName, args)
-- 	if self._isPrintLog then
-- 		print(funcName.."@"..os.clock(), table.get_table_str(args))
-- 	end

-- 	self._protocolMgr:receiveRpc(funcName, args)
-- end

Crpc.SetPrintLog = function(self, isPrint)
	if isPrint then
		self._isPrintLog = true
	else
		self._isPrintLog = false
	end

	GlobalCfg.IsRpcLog = isPrint
end

Crpc.IsPrintLog = function(self)
	return self._isPrintLog
end